<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    </head>
    <?php
        $sura = isset($_GET['sura']) ? $_GET['sura'] : 1;
        $sql  = "SELECT * FROM sura WHERE `index` = '".$sura."'";
        $namaSura = '';
        foreach ($pdo->query($sql) as $row) :
            $namaSura = $row['name_indonesia'];
        endforeach;
    ?>
        <style type="text/css">
            table td.ayat-arabic {
                font-family: myFirstFont;
                text-align: center;
                font-size: 25px;
                letter-spacing: 1px;
                vertical-align: middle;
                weight: 10%;
                line-height:40px;
            }
            table td.ayat {
                text-align: center;
                vertical-align: middle;
                weight: 10%;
                line-height:40px;
            }
            table td .ayat-arabic {
                font-family: myFirstFont;
                text-align: center;
                font-size: 25px;
                letter-spacing: 1px;
                vertical-align: middle;
                weight: 10%;
                line-height:40px;
            }
        </style>
    <body>
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">
                    <a href="?menu=quran">AL QUR'AN</a>
                <?php if ($namaSura !== ''): ?>
                    <?php echo ' - '.$namaSura; ?>
                <?php endif; ?>
                </h1>
            </div>
        </div>
        <?php
        $dataPerHalaman = 10;
        if (!empty($_GET['page'])) {
            $hal       = $_GET['page'] - 1;
            $mulaiAwal = $dataPerHalaman * $hal;
        } elseif (!empty($_GET['page']) and $_GET['page'] == 1) {
            $mulaiAwal = 0;
        } elseif (empty($_GET['page'])) {
            $mulaiAwal = 0;
        }
        ?>
        <table class="table table-striped">
            <thead>
                <tr>
                    <!--th class="head-table">Ayat</th>
                    <th class="head-table">Terjemah Indonesia</th-->
                    <th class="head-table">Qur'an</th>
                    <!--th class="head-table">Ayat</th-->
                </tr>
            </thead>
            <tbody>
                <?php
                $sql = "SELECT qtu.sura, qtu.aya, qtu.text,
                        (SELECT text FROM quran_text_indonesia qti 
                         WHERE qti.sura = qtu.sura AND qti.aya = qtu.aya) as text_indonesia 
                        FROM quran_text_uthmani qtu WHERE sura = '".$sura."' limit ".$mulaiAwal.", ".$dataPerHalaman;
                foreach ($pdo->query($sql) as $row) :
                ?>
                    <tr>
                        <td>
                            <div class="arabic"><?php echo angkaToArabic($row['aya']).'. '.$row['text'] ?></div>
                            <div class="indonesia"><?php echo $row['aya'].'. '.$row['text_indonesia'] ?></div>
                        </td>
                    </tr>
                <?php
                endforeach;
                ?>
                <?php
                /*
                $sql = "SELECT qtu.sura, qtu.aya, qtu.text,
                        (SELECT text FROM quran_text_indonesia qti 
                         WHERE qti.sura = qtu.sura AND qti.aya = qtu.aya) as text_indonesia 
                        FROM quran_text_uthmani qtu WHERE sura = '".$sura."' limit ".$mulaiAwal.", ".$dataPerHalaman;
                foreach ($pdo->query($sql) as $row) :
                ?>
                    <tr>
                        <td class="ayat"><?php echo $row['aya'] ?></td>
                        <td class="indonesia"><?php echo $row['text_indonesia'] ?></td>
                        <td class="arabic"><?php echo $row['text'] ?></td>
                        <td class="ayat-arabic"><?php echo angkaToArabic($row['aya']) ?></td>
                    </tr>
                <?php
                endforeach;
                */
                ?>
            </tbody>
        </table>
        <div class="pagination">
            <ul>
            <?php
            $jumlahData = 0;
            $sql = "SELECT COUNT(*) FROM quran_text_uthmani WHERE sura = '".$sura."'";
            $result = $pdo->prepare($sql); 
            $result->execute(); 
            $jumlahData = $result->fetchColumn();
            if ($jumlahData > $dataPerHalaman) {
                $banyakHalaman = ceil($jumlahData/$dataPerHalaman);
                $halaman = isset($_GET['page']) ? $_GET['page'] : 1;
                $halaman = (int)$halaman;
                if ($halaman > 1) {
                    echo '<li><a href="?menu=quran-content&sura='.$sura.'&page='.($halaman - 1).'">Prev</a></li>';
                } else {
                    echo '<li class="disabled"><a href="#">Prev</a></li>';
                }
                
                for($page = 1; $page <= $banyakHalaman; $page++) {
                    
                    if ($page == 1) {
                        if ($page == $halaman) {
                            echo '<li class="active"><a href="?menu=quran-content&sura='.$sura.'&page='.$page.'">'.$page.'</a></li>';
                        } else {
                            echo '<li><a href="?menu=quran-content&sura='.$sura.'&page='.$page.'">First</a></li>';
                        }
                    } 
                    
                    if ($page == $banyakHalaman) {
                        if ($page == $halaman) {
                            echo '<li class="active"><a href="?menu=quran-content&sura='.$sura.'&page='.$page.'">'.$page.'</a></li>';
                        } else {
                            echo '<li><a href="?menu=quran-content&sura='.$sura.'&page='.$page.'">Last</a></li>';
                        }
                    }
                    
                    if (($page >= $halaman - 3) && ($page <= $halaman + 3)) {
                        if ($page == $banyakHalaman) {
                            continue;
                        }
                        if ($page == 1) {
                            continue;
                        }
                        if ($page !== 1 || $page !== $banyakHalaman) {
                            if ($page == $halaman) {
                                echo '<li class="active"><a href="?menu=quran-content&sura='.$sura.'&page='.$page.'">'.$page.'</a></li>';
                            } else {
                                echo '<li><a href="?menu=quran-content&sura='.$sura.'&page='.$page.'">'.$page.'</a></li>';
                            }
                        } 
                        
                    }
                }
                
                if ($halaman < $banyakHalaman) {
                    echo '<li><a href="?menu=quran-content&sura='.$sura.'&page='.($halaman + 1).'">Next</a></li>';
                } elseif ($halaman >= $banyakHalaman) {
                    echo '<li class="disabled"><a href="#">Next</a></li>';
                }
            }
            ?>
            </ul>
        </div>
    </body>
</html>


